/*
 * @Author: intellectual-seeker 3336339701@qq.com
 * @Date: 2025-03-13 22:18:18
 * @LastEditors: intellectual-seeker 3336339701@qq.com
 * @LastEditTime: 2025-03-24 12:38:29
 * @FilePath: /code/lesson24/pipe/testpipe.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>

void writer(int wfd)
{
    char* str = "hello father! this is chlid";
    char buffer[128];
    int pid = getpid();
    int cnt = 0;

    while(1)
    {
        //1. 写端写的慢，读端正常读取
        // snprintf(buffer, sizeof(buffer), "%s, pid = %d, count = %d\n", str, pid, cnt++);
        // write(wfd, buffer, sizeof(buffer));

        // sleep(5);

        //2. 写端一直写，读端不读（计算管道大小）
        char c = 'A';
        write(wfd, &c, 1);

        cnt++;
        printf("cnt = %d\n", cnt);

        //3. 写端关闭，读端一直读
        // sleep(1);

        // char c = 'A';
        // write(wfd, &c, 1);

        // cnt++;
        // printf("cnt = %d\n", cnt);
        // if(cnt == 10) 
        //     break;

        //4. 读端关闭，写端一直写
        // sleep(1);
        // char c = 'A';
        // write(wfd, &c, 1);
        // cnt++;
        // printf("cnt: %d\n", cnt);
    }
    
    //3.
    //close(wfd);
}

void reader(int rfd)
{
    char buffer[1024];
    int cnt = 10;//4.
    while(1)
    {
        //1. 写慢读快
        // size_t n = read(rfd, buffer, sizeof(buffer) - 1);
        // (void) n;
        // printf("father get a message: %s", buffer);
        // sleep(1);

        //2. 一直写不读;
        sleep(100);

        //3. 一直读，写关闭
        // size_t n = read(rfd, buffer, sizeof(buffer) - 1);

        // if(n > 0)
        //     printf("father get a message: %s, n: %ld\n", buffer, n);
        // else if(n == 0)
        // {
        //     printf("read pipe done, read file done!\n");
        //     break;
        // }
        // else
        //     break;

        //4. 读关闭，一直写
        // size_t n = read(rfd, buffer, sizeof(buffer) - 1);

        // if(n > 0)
        //     printf("father get a message: %s, n: %ld\n", buffer, n);
        // else if(n == 0)
        // {
        //     printf("read pipe done, read file done!\n");
        //     break;
        // }
        // else
        //     break;

        // cnt--;
        // if(cnt == 0) break;
    }

    //3. 4.
    // close(rfd);
    // printf("read endpoint close!\n");

}

int main()
{
    int pipefd[2];
    int ret = pipe(pipefd);
    if(ret < 0) return 1;

    printf("pipefd[0] = %d, pipefd[1] = %d\n", pipefd[0]/*read*/, pipefd[1]/*write*/);

    pid_t fd = fork();

    if(fd == 0)//子进程
    {
        //child 1
        close(pipefd[0]);

        writer(pipefd[1]);

        exit(0);
    }
    else
    {
        //father 0
        close(pipefd[1]);

        reader(pipefd[0]);
    }

    //wait(NULL);//回收子进程

    int status = 0;
    int rid = waitpid(fd, &status, 0);
    if(rid == fd)
    {
        printf("exit code: %d, exit signal: %d\n", WEXITSTATUS(status), status&0x7F);
    }

    return 0;
}
